﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace PixelWarfare
{
    /// <summary>
    /// Static class to keep track of all the maps
    /// </summary>
    static class Map
    {
        #region Fields

        private static List<Bitmap> maps;
        private static List<string> mapNames;

        private static RandomOrder rand;

        #endregion

        #region Public Methods

        /// <summary>
        /// Loads all maps from img/*.bmp if the img directory exists.
        /// Adds them into the maps and mapNames lists along with the
        /// empty map.
        /// </summary>
        public static void LoadMaps()
        {
            string[] filePaths;

            // Incase the directory doesn't exist.
            try
            {
                filePaths = Directory.GetFiles("img/", "*.bmp");
            }
            catch (Exception ex)
            {
                // Stop compiler warning.
                ex.ToString();

                filePaths = new string[0];
                MessageBox.Show("The img directory was not found, no extra maps will be loaded.");
            }

            maps = new List<Bitmap>();
            mapNames = new List<string>();
            Bitmap temp;

            // Add the empty map.
            temp = new Bitmap(500, 500);
            temp.MakeTransparent(Color.Black);
            maps.Add(temp);
            mapNames.Add("None");

            for (int i = 0; i < filePaths.Length; i++)
            {
                temp = new Bitmap(filePaths[i]);

                // Only add the file if its the correct size.
                if (temp.Width == 500 && temp.Height == 500)
                {
                    temp.MakeTransparent(Color.Black);
                    maps.Add(temp);

                    // Remove directory "img/" and ".bmp" extension
                    mapNames.Add(filePaths[i].Substring(4, filePaths[i].Length - 8));
                }
                else
                {
                    MessageBox.Show(filePaths[i] + " is invalid because it is not 500x500 pixels.");
                }
            }
        }

        /// <summary>
        /// Returns the map at the given index.
        /// </summary>
        /// <param name="index">Index of the map you requested.</param>
        /// <returns>The bitmap representation of the map.</returns>
        public static Bitmap GetMap(int index)
        {
            return maps[index];
        }

        /// <summary>
        /// Returns the map name at the given index.
        /// </summary>
        /// <param name="index">Index of the map name you requested.</param>
        /// <returns>The name of the map.</returns>
        public static string GetName(int index)
        {
            return mapNames[index];
        }

        /// <summary>
        /// Resets the random mechanism in map so all maps
        /// have a chance to appear.
        /// </summary>
        public static void ResetRandom()
        {
            rand = new RandomOrder(Count);
        }

        /// <summary>
        /// Gets the next map, ensures all maps a chosen before
        /// repeating maps.
        /// </summary>
        /// <returns>The randomly chosen map.</returns>
        public static Bitmap NextRandom()
        {
            int newIndex = rand.Next();

            // If all maps have already been chosen.
            if (newIndex == -1)
            {
                rand = new RandomOrder(Count);
                newIndex = rand.Next();
            }

            return maps[newIndex];
        }

        #endregion

        #region Properties

        /// <summary>
        /// Returns the number of maps.
        /// </summary>
        public static int Count
        {
            get { return maps.Count; }
        }

        #endregion

    }
}
